
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.InvocationTargetException;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

@SuppressWarnings("serial")
public class UserList extends JList implements MouseListener, ListSelectionListener, ActionListener
{
	ChatPanel chatPanel;
	ChatPopup popup;
	DefaultListModel users;
	int minWidth;
	String usr;
	
	public UserList(ChatPanel panel)
	{
		super();
		users = new DefaultListModel();
		popup = new ChatPopup(this);
		addMouseListener(this);
		addListSelectionListener(this);
		
		setModel(users);
		setLayoutOrientation(VERTICAL);
		setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		TitledBorder title = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Users:");
		System.out.println(title.getTitleFont().getSize());
		title.setTitleFont(title.getTitleFont().deriveFont(14.0F).deriveFont(Font.BOLD));
		setBorder(title);
		setBackground(null);
		minWidth = 100;
		setMinimumSize(new Dimension(minWidth,0));
		chatPanel = panel;
		setFont(getFont().deriveFont(Font.PLAIN));
		users.addElement("");
		setSelectionBackground(null);
		setSelectionForeground(null);
		setFocusable(false);
	}
	
	public void addUser(String name)
	{
		if(!SwingUtilities.isEventDispatchThread())
		{
			this.usr = name;
				try {
					SwingUtilities.invokeAndWait(new Runnable()
					{
						@Override
						public void run() 
						{
							users.removeElement("");
							users.addElement(usr);
							users.addElement("");
						}
					});
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		} else
		{
			users.removeElement("");
			users.addElement(name);
			users.addElement("");
		}
	}
	
	public void removeUser(String name)
	{
		users.removeElement(name);
	}

	@Override
	public void mouseClicked(MouseEvent e) 
	{
	}

	@Override
	public void mouseEntered(MouseEvent e) {}

	@Override
	public void mouseExited(MouseEvent e) {}

	@Override
	public void mousePressed(MouseEvent e) 
	{
	}

	@Override
	public void mouseReleased(MouseEvent e) 
	{
		if(e.getButton() == MouseEvent.BUTTON3)
		{
			displayPopup(e);
		}
	}
	
	public void displayPopup(MouseEvent e)
	{
		String index = null;
		int loc = locationToIndex(e.getPoint());
		if(loc != users.getSize()-1)
			index = users.get(loc).toString();
		if(index != null && !chatPanel.username.equals(index))
		{
			setSelectedIndex(loc);
			if(chatPanel.isIgnoring(index))
				popup.showListen();
			else
				popup.showIgnore();
			popup.show(e.getComponent(), e.getX(), e.getY());
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent e) 
	{
		if(getSelectedIndex() == users.getSize()-1)
		{
			getSelectionModel().clearSelection();
			clearSelection();
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		String username = getSelectedValue().toString();
		if(e.getActionCommand().equalsIgnoreCase("poke"))
		{
			chatPanel.sendPoke(username);
		} else if(e.getActionCommand().equalsIgnoreCase("whisper"))
		{
			chatPanel.sendWhisper(username);
		} else if(e.getActionCommand().equalsIgnoreCase("ignore"))
		{
			chatPanel.sendIgnore(username);
		} else if(e.getActionCommand().equalsIgnoreCase("listen"))
		{
			chatPanel.cancelIgnore(username);
		} else
		{
			chatPanel.game.startGame(username);
		}
	}
}
